/**
 * 作者：陈文斌 on 2019/1/11.
 * 邮箱：chen_wennbin@163.com
 * 版本：v1.0
 */

import 'package:crm/constant/api.dart';
import 'package:crm/tools/http_util.dart';
import 'package:crm/model/Company/company_category_model.dart';

class IndustryCategoryVM {
  getIndustryCategories(
    int page,
    List<CompanyCategoryInfoModel> industryCategories, {
    Function callback,
    Function errorCallback,
  }) {
    final int _take = 30; // 每次请求10条信息
    final int _skip = page * _take; // 从第skip + 1条开始请求

    Map<String, String> data = {
      'skip': '${_skip}',
      'take': '$_take',
      'fields': 'SubCategories',
      'TypeId': '6e3cecaf-4d35-e711-80e4-da42ba972ebd',
    };

    HttpUtil.get(
      Api.CompanyCategories,
      (response) {
        // 请求成功回调
        var _result = response;
        if (page == 0) industryCategories = List<CompanyCategoryInfoModel>();

        for (var newsItem in _result) {
          industryCategories.add(CompanyCategoryInfoModel.fromJson(newsItem));
        }

        if (callback != null) {
          callback(industryCategories);
        }
      },
      params: data,
      errorCallback: (errMsg) {
        if (errorCallback != null) ;
        errorCallback(errMsg);
      },
    );
  }
}
